﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using WiimoteLib;

namespace TayoukiNameSpace
{   
    public partial class GameForm : Form
    {       

        string clock12 = @"C:\\Images\\clock_12.png";
        string clock1 = @"C:\\Images\\clock_1.png";
        string clock2 = @"C:\\Images\\clock_2.png";
        string clock3 = @"C:\\Images\\clock_3.png";
        string clock4 = @"C:\\Images\\clock_4.png";
        string clock5 = @"C:\\Images\\clock_5.png";
        string clock6 = @"C:\\Images\\clock_6.png";
        string clock7 = @"C:\\Images\\clock_7.png";
        string clock8 = @"C:\\Images\\clock_8.png";
        string clock9 = @"C:\\Images\\clock_9.png";
        string clock10 = @"C:\\Images\\clock_10.png";
        string clock11 = @"C:\\Images\\clock_11.png";

        string smiley_sad = @"C:\\Images\\sad_face.jpg";
        string smiley_happy = @"C:\\Images\\happy_face.jpg";
        string congrats = @"C:\\Images\\congrats.jpg";

        string normal = @"C:\\Images\\normal.jpg";
        string move_right = @"C:\\Images\\move_right.png";
        string move_left = @"C:\\Images\\move_left.png";        
        

        static int dials = 12;
        static int counter = 0;
        static int current = 0;
        static int done = 0;
        static int end = 5;
        static int difference = Math.Abs(end - current);
        string[] paths = new string[12];

        Wiimote wm = new Wiimote();
        private bool btnPressed;                

            
        public GameForm()
        {
            InitializeComponent();
            btnPressed = false;            

            //initialize the path array
            paths[0] = clock12;
            paths[1] = clock1;
            paths[2] = clock2;
            paths[3] = clock3;
            paths[4] = clock4;
            paths[5] = clock5;
            paths[6] = clock6;
            paths[7] = clock7;
            paths[8] = clock8;
            paths[9] = clock9;
            paths[10] = clock10;
            paths[11] = clock11;

            //add Event Handler to the form
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
        }

        

        


        // Detect all numeric characters at the form level and consume 1, 
        // 4, and 7. Note that Form.KeyPreview must be set to true for this
        // event handler to be called. 48-57
        void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 1 && e.KeyChar <= 255 && done == 0)
            {
                //l= 108 (right)
                //k= 107 (left)

                //this.textBox1.Text = "Detected Arrow Press";

                switch (e.KeyChar)
                {
                    //right key detected
                    case (char)108:
                        //update counter
                        counter++;
                        if (counter == dials)
                        {
                            counter = 0;
                        }

                        //reached the end
                        if (counter == end)
                        {
                            this.pictureBox2.Image = Image.FromFile(paths[counter]);
                            this.pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display happy smiley
                            this.pictureBox1.Image = Image.FromFile(congrats);
                            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display normal wii
                            this.pictureBox3.Image = Image.FromFile(normal);
                            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update text
                            this.textBox1.Text = "Congratulations! " + e.KeyChar.ToString();

                            //to make sure future presses are not considered
                            done = 1;

                            break;
                        }

                        this.textBox1.Text = "Detected " + e.KeyChar.ToString();
                        this.pictureBox2.Image = Image.FromFile(paths[counter]);
                        this.pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;

                        //figure out what image to display in the other two picture boxes
                        int new_diff = 0;
                        if (counter <= 6)
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(12 - end) + Math.Abs(0 - counter)));
                        }
                        else
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(0 - end) + Math.Abs(12 - counter)));
                        }

                        if (new_diff <= difference)
                        {
                            //display happy smiley
                            this.pictureBox1.Image = Image.FromFile(smiley_happy);
                            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display normal wii
                            this.pictureBox3.Image = Image.FromFile(normal);
                            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            this.textBox1.Text = "Correct: Old diff= " + difference + " New diff= " + new_diff;
                        }
                        else //should be moving left
                        {
                            //display sad smiley
                            this.pictureBox1.Image = Image.FromFile(smiley_sad);
                            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display move_left wii
                            this.pictureBox3.Image = Image.FromFile(move_left);
                            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            this.textBox1.Text = "Move left: Old diff= " + difference + " New diff= " + new_diff;
                        }

                        //update difference
                        difference = new_diff;
                                              
                        break;

                    //left key detected
                    case (char)107:
                        //update counter
                        counter--;
                        if (counter < 0)
                        {
                            counter = 11;
                        }

                        //reached the end
                        if (counter == end)
                        {
                            this.pictureBox2.Image = Image.FromFile(paths[counter]);
                            this.pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display happy smiley
                            this.pictureBox1.Image = Image.FromFile(congrats);
                            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display normal wii
                            this.pictureBox3.Image = Image.FromFile(normal);
                            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update text
                            this.textBox1.Text = "Congratulations! " + e.KeyChar.ToString();

                            //to make sure future presses are not considered
                            done = 1;

                            break;
                        }

                        this.textBox1.Text = "Detected " + e.KeyChar.ToString();
                        this.pictureBox2.Image = Image.FromFile(paths[counter]);
                        this.pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;

                        //figure out what image to display in the other two picture boxes
                        new_diff = 0;
                        if (counter <= 6)
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(12 - end) + Math.Abs(0 - counter)));
                        }
                        else
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(0 - end) + Math.Abs(12 - counter)));
                        }

                        if (new_diff <= difference)
                        {
                            //display happy smiley
                            this.pictureBox1.Image = Image.FromFile(smiley_happy);
                            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display normal wii
                            this.pictureBox3.Image = Image.FromFile(normal);
                            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            this.textBox1.Text = "Correct: Old diff= " + difference + " New diff= " + new_diff;
                        }
                        else //should be moving right
                        {
                            //display sad smiley
                            this.pictureBox1.Image = Image.FromFile(smiley_sad);
                            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display move_left wii
                            this.pictureBox3.Image = Image.FromFile(move_right);
                            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            this.textBox1.Text = "Move right: Old diff= " + difference + " New diff= " + new_diff;
                        }

                        //update difference
                        difference = new_diff;
                        
                        break;
                }
            }
        }
           

        
        private void Form1_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            this.WindowState = FormWindowState.Maximized;
            this.pictureBox2.Image = Image.FromFile(clock12);
            this.pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;

            this.pictureBox1.Image = Image.FromFile(smiley_happy);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;

            this.pictureBox3.Image = Image.FromFile(normal);
            this.pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;

            this.textBox1.Text = "Move the handle to 5";

            initializeWiiMote();
       
        }

        private void initializeWiiMote()
        {
            wm.WiimoteChanged += new EventHandler<WiimoteChangedEventArgs>(wm_WiimoteChanged);
            try
            {
                wm.Connect();
            }
            catch (Exception)
            {
                //DO SOMETHING
                throw;
            }
            
            wm.SetReportType(InputReport.IRAccel, true);
            wm.SetLEDs(false, true, false, false);
        }

        private void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
        {
            WiimoteState wms = args.WiimoteState;            

            if (wms.ButtonState.Left)
            {
                
                if (!btnPressed)
                {
                    System.Console.WriteLine("Pressed left");
                    SendKeys.SendWait("k");
                }
                btnPressed = true;
            }

            else if (wms.ButtonState.Right)
            {
                if (!btnPressed)
                {
                    SendKeys.SendWait("l");
                }
                btnPressed = true;
            }           

            else
            {
                btnPressed = false;
            }
        }

        
        
    }
}
